package cn.micro.system.manage.annotate.impl;


import cn.micro.system.manage.annotate.UniqueUser;
import cn.micro.system.manage.entity.SysDimUser;
import cn.micro.system.manage.service.SysDimUserService;
import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class UserValidation  <T extends Annotation> implements ConstraintValidator<T, SysDimUser> {

    protected Predicate<SysDimUser> predicate = c -> true;

    @Resource
    protected SysDimUserService sysDimUserService;

    @Override
    public boolean isValid(SysDimUser sysDimUser, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimUserService == null || predicate.test(sysDimUser);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class UniqueUserValidate extends UserValidation<UniqueUser>{
        @Override
        public void initialize(UniqueUser constraintAnnotation) {
            predicate = c -> sysDimUserService.getByUsername(c).isEmpty();
        }
    }

}
